<template>
  <div class="strategy-like-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h2>
            <i class="icon-heart">⭐</i>
            我收藏的攻略
          </h2>
          <div class="header-subtitle">收藏精彩的旅行攻略</div>
        </div>
        <div class="total-count">
          <span class="count-number">{{ strategies.length }}</span>
          <span class="count-text">条收藏</span>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <span class="loading-text">加载精彩内容中...</span>
    </div>

    <!-- 空状态 -->
    <div v-else-if="strategies.length === 0" class="empty-state">
      <div class="empty-icon">
        <i class="icon-empty">📋</i>
      </div>
      <h3 class="empty-title">还没有收藏的攻略</h3>
      <p class="empty-description">快去发现那些精彩的旅行攻略吧！</p>
      <router-link to="/homepage/strategy" class="explore-btn">
        <i class="icon-explore">🔍</i>
        探索攻略
      </router-link>
    </div>

    <!-- 攻略列表 -->
    <div v-else class="strategy-list">
      <div v-for="strategy in strategies" :key="strategy.collectId" class="strategy-card">
        <!-- 整个卡片可点击区域 -->
        <div class="card-clickable-area" @click="goToDetail(strategy.id)">
          <!-- 图片容器 -->
          <div class="strategy-img-container">
            <img :src="strategy.images[0] || defaultImg" class="strategy-img" alt="攻略图片" />
            <div class="image-overlay"></div>
            <div class="like-badge">
              <i class="badge-icon">⭐</i>
            </div>
          </div>
          
          <!-- 内容区域 -->
          <div class="strategy-content">
            <h3 class="strategy-title">{{ strategy.name }}</h3>
            <p class="strategy-description">{{ strategy.content }}</p>
            
            <!-- 元信息 -->
            <div class="strategy-meta">
              <div class="meta-item">
                <i class="meta-icon">📍</i>
                <span>{{ strategy.scenic }}</span>
              </div>
              <div class="meta-item">
                <i class="meta-icon">📅</i>
                <span>{{ formatTime(strategy.favoriteTime) }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 底部操作区 -->
        <div class="strategy-footer">
          <!-- 作者信息 -->
          <div class="author-info">
            <img :src="strategy.userAvatar" class="author-avatar" alt="作者头像" />
            <span class="author-name">{{ strategy.userName }}</span>
          </div>
          
          <!-- 操作按钮 -->
          <div class="strategy-actions">
            <button class="like-btn active" @click.stop="toggleCollect(strategy)">
              <i class="like-icon">⭐</i>
              <span>{{ strategy.collectCount }}</span>
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "StrategyCollec",
  data() {
    return {
      strategies: [],
      loading: true,
      defaultImg: "https://img1.baidu.com/it/u=2331851122,2068186006&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333"
    };
  },
  async mounted() {
    await this.fetchStrategyCollects();
  },
  methods: {
    formatTime(time) {
      if (!time) return '';
      return time.split('T')[0];
    },

    async fetchStrategyCollects() {
      this.loading = true;
      // 1. 获取当前用户loginId
      let loginId = localStorage.getItem('loginId');
      if (!loginId) {
        this.strategies = [];
        this.loading = false;
        return;
      }
      // 2. 通过loginId查userId
      let userId = null;
      try {
        const userRes = await fetch(`http://localhost:8080/mw/UserByLoginId?loginId=${loginId}`);
        const userData = await userRes.json();
        userId = userData.userId;
        if (!userId) {
          this.strategies = [];
          this.loading = false;
          return;
        }
      } catch (e) {
        this.strategies = [];
        this.loading = false;
        return;
      }
      // 3. 用userId查所有收藏的攻略
      try {
        const res = await fetch(`http://localhost:8080/strategyCollect/listByUser?userId=${userId}`);
        const userCollects = await res.json();
        const strategyList = [];
        for (const item of userCollects) {
          let detail = {};
          let images = [];
          let collectCount = 0;
          let authorInfo = {};
          let scenicName = "";
          try {
            // 获取攻略详情（含作者userId和scenicSpotId）
            const detailRes = await fetch(`http://localhost:8080/ts/detail/${item.strategyId}`);
            const detailData = await detailRes.json();
            detail = detailData.detail || {};
            images = (detailData.images || []).map(img => img.strategyImageLink).filter(Boolean);

            // 获取作者信息
            if (detail.userId) {
              const authorRes = await fetch(`http://localhost:8080/mw/UserById?userId=${detail.userId}`);
              authorInfo = await authorRes.json();
            }
          } catch (e) {}
          try {
            // 获取该攻略被收藏总数
            const countRes = await fetch(`http://localhost:8080/strategyCollect/list?strategyName=${encodeURIComponent(detail.strategyName || '')}`);
            const allCollects = await countRes.json();
            collectCount = (allCollects.list || []).filter(c => c.strategyId === item.strategyId).length;
          } catch (e) {
            collectCount = 0;
          }
          strategyList.push({
            collectId: item.id,
            id: item.strategyId,
            name: item.strategyName || detail.strategyName || "",
            scenic: detail.scenic_area_name || "",
            content: (detail.strategyContent || "").substring(0, 80) + "...",
            userAvatar: authorInfo.userAvatar || detail.userAvatar || "",
            userName: authorInfo.userName || detail.userName || "",
            images: images.length ? images : [this.defaultImg],
            collected: true,
            collectCount,
            favoriteTime: item.favoriteTime
          });
        }
        this.strategies = strategyList;
      } catch (e) {
        this.strategies = [];
      } finally {
        this.loading = false;
      }
    },
    async toggleCollect(strategy) {
      // 只能取消收藏
      if (strategy.collected) {
        try {
          await fetch(`http://localhost:8080/strategyCollect/delete/${strategy.collectId}`, {
            method: "DELETE"
          });
          this.$message.success('取消收藏成功');
          await this.fetchStrategyCollects();
        } catch {
          this.$message.error('取消收藏失败，请重试');
        }
      }
    },
    goToDetail(strategyId) {
      // 跳转到攻略详情页
      this.$router.push(`/homepage/strategy/detail/${strategyId}`);
    }
  }
};
</script>

<style scoped>
.strategy-like-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  margin-bottom: 32px;
}

.header-content {
  background: white;
  padding: 24px 32px;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-heart {
  font-size: 24px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.header-subtitle {
  font-size: 14px;
  color: #7f8c8d;
  margin: 0;
}

.total-count {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(135deg, #a6a6e4 0%, #c3aadd 100%);
  color: white;
  padding: 16px 20px;
  border-radius: 12px;
  min-width: 80px;
}

.count-number {
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.count-text {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 4px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #6b7280;
  font-weight: 500;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 16px;
  padding: 60px 40px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 24px;
  opacity: 0.6;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
}

.empty-description {
  font-size: 16px;
  color: #6b7280;
  margin: 0 0 32px 0;
  line-height: 1.5;
}

.explore-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.explore-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

/* 攻略列表 */
.strategy-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.strategy-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
}

.strategy-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.card-clickable-area {
  cursor: pointer;
}

.strategy-img-container {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.strategy-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.strategy-card:hover .strategy-img {
  transform: scale(1.1);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.3) 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.strategy-card:hover .image-overlay {
  opacity: 1;
}

.like-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 6px 12px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #e74c3c;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.badge-icon {
  font-size: 12px;
  animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

.strategy-content {
  padding: 20px;
}

.strategy-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.strategy-description {
  font-size: 14px;
  color: #6b7280;
  line-height: 1.6;
  margin: 0 0 16px 0;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.strategy-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #9ca3af;
}

.meta-icon {
  font-size: 14px;
}

.strategy-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: #f8fafc;
  border-top: 1px solid #e2e8f0;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.author-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #e2e8f0;
}

.author-name {
  font-size: 14px;
  font-weight: 500;
  color: #4b5563;
}

.strategy-actions {
  display: flex;
  gap: 8px;
}

.like-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.like-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.like-btn.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.like-icon {
  font-size: 14px;
  animation: like-pulse 0.6s ease-in-out;
}

@keyframes like-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .strategy-like-container {
    padding: 16px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .strategy-list {
    grid-template-columns: 1fr;
  }
  
  .strategy-footer {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 20px;
  }
  
  .header-left h2 {
    font-size: 24px;
  }
  
  .strategy-content {
    padding: 16px;
  }
}
</style>